package progetto1.csm.implementation;

import java.util.Random;

import progetto1.csm.core.Statistics;
import sde.actor.Simulation;

public class CSMSystem implements progetto1.csm.core.System {
	
	private Simulation simulation;
	private CSMWatcher watcher = new CSMWatcher();
	private Source source = new Source(watcher, CSMSystemElements.SYSTEM);
	private Station station1 = new Station(watcher, CSMSystemElements.STATION1),
			station2 = new Station(watcher, CSMSystemElements.STATION2),
			station3 = new Station(watcher, CSMSystemElements.STATION3),
			station4 = new Station(watcher, CSMSystemElements.STATION4);
	private Router router = new Router(watcher, null);
	private final double q0 = 0.2, q1 = 0.3, q2 = 0.2, q3 = 0.3;
	private CSMScenario scenario;
	
	protected CSMSystem(CSMScenario scenario) {
		this.scenario = scenario;
		simulation = new Simulation(SystemFactory.getTimeSimulation());
	}
	
	private void inizialize(int k){
		simulation.reboot();//metodo aggiunto per non riscreare la macchina di controllo
		Random r = scenario.d0.getRandomGenerator();//prendo il random generator da una distribuzione
		watcher.init();
		router.init(r, source, station2, station3, station4, q0, q1, q2, q3);
		source.init(k, scenario.d0, station1);
		station1.init(scenario.d1, router);
		station2.init(scenario.d2, station1);
		station3.init(scenario.d3, station1);
		station4.init(scenario.d4, station1);
	}
	
	@Override
	public Statistics simulate(int k) {
		inizialize(k);
		simulation.controller();
		return watcher.getResult(k);
	}
}
